﻿var Class = {
    create: function() {
        return function() {
            //调用构造函数
            this._construct.apply(this, arguments);
            this.addListener = function(fn, cb, args, obj){};
            this.removeListener = function(fn, cb){};
        }
    }
}
//循环查找XML节点
function selectNodes(parentNode, xPath, arr)
{
    var arrPath = xPath.split("/");
    var tag = arrPath[0];
    for (var i = 0; i < parentNode.childNodes.length; i++)
    {
        var node = parentNode.childNodes[i];
        if (node.nodeType == 1 && node.tagName == tag)
        {
            if (arrPath.length > 1)
            {
                selectNodes(node, xPath.substring(xPath.indexOf("/") + 1), arr);
            }
            else
            {
                arr.push(new XMLElem(node));
            }
        }
    }
}
function selectSingleNode(parentNode, xPath)
{
    var arrPath = xPath.split("/");
    var tag = arrPath[0];
    for (var i = 0; i < parentNode.childNodes.length; i++)
    {
        var node = parentNode.childNodes[i];
        if (node.nodeType == 1 && node.tagName == tag)
        {
            if (arrPath.length > 1)
            {
                return selectSinleNode(node, xPath.substring(xPath.indexOf("/") + 1));
            }
            else
            {
                return new XMLElem(node);
            }
        }
    }
    return null;
} 
var XMLDoc = Class.create();
XMLDoc.prototype = {
    xmlDoc: null,
    _construct:function(doc){
        if (doc){
            this.xmlDoc = doc;
        }else{
            try{
                this.xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
            }
            catch(e){
                try{
                    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                }
                catch(o){
                    if(document.implementation 
                        && document.implementation.createDocument
                        && document.implementation.hasFeature("XPath", "3.0")){
                        this.xmlDoc = document.implementation.createDocument('','',null);
                    }
                }
            }
        }
        this.xmlDoc.async = false;
    },
    loadXML:function(xmlString){
        var xmldoc = this.xmlDoc;
        if ($.browser.msie){
            xmldoc.loadXML(xmlString);
        }else{
            var childNodes = xmldoc.childNodes;
            for (var i = childNodes.length - 1; i >= 0; i--){
                xmldoc.removeChild(childNodes[i]);
            };
            var dp = new DOMParser();
            var newDOM = dp.parseFromString(xmlString, "text/xml");
            var newElt = xmldoc.importNode(newDOM.documentElement, true);
            xmldoc.appendChild(newElt);
        }
    },
    selectNodes:function(xPath, xNode){
        var aResult = [];
        xPath = xPath.replace("//","/");
        xNode = xPath.startsWith("/") ? this.xmlDoc : xNode;
        xPath = xPath.trim("/");
        xNode = xNode ? xNode : this.xmlDoc;
        if ($.browser.msie){
            var ns = xNode.selectNodes(xPath);
            for (var i = 0, l = ns.length; i < l; i++){
                aResult.push(new XMLElem(ns[i]));
            }
        }else{
            selectNodes(xNode, xPath, aResult);
        }
        return aResult;
    },
    selectSingleNode:function(xPath, xNode){
        var ret = null;
        xPath = xPath.replace("//","/");
        xNode = xPath.startsWith("/") ? this.xmlDoc : xNode;
        xNode = xNode ? xNode : this.xmlDoc;
        xPath = xPath.trim("/");
        if (Ctn.Browser.ie()){
            var n = xNode.selectSingleNode(xPath);
            if (n != null){
                ret = new XMLElem(n);
            }
        }else{
            ret = selectSingleNode(xNode, xPath);
        }
        return ret;
    },
    xml:function(){
        return this.xmlDoc.xml == undefined ? new XMLSerializer().serializeToString(this.xmlDoc) : this.xmlDoc.xml; 
    },
    val:function(v){
        return this.documentElement().xml(v);
    },
    text:function(v){
        return this.documentElement().text(v);
    },
    attr:function(n, v){
        return this.documentElement().attr(n, v);
    },
    documentElement:function(){
        return new XMLElem(this.xmlDoc.documentElement);
    },
    validate: function(){
    }
}

var XMLElem = Class.create();
XMLElem.prototype = {
    xmlElem:null,
    _construct: function(elem){
        this.xmlElem = elem;
    },
    selectNodes:function(xPath){
        return this.ownerDocument().selectNodes(xPath, this.xmlElem);
    },
    selectSingleNode:function(xPath){
        return this.ownerDocument().selectSingleNode(xPath, this.xmlElem);
    },
    xml:function(){
        return this.xmlElem.xml == undefined ? new XMLSerializer().serializeToString(this.xmlElem) : this.xmlElem.xml; 
    },
    val:function(){
        var v = this.xml();
        return v.substring(v.indexOf(">") + 1, v.lastIndexOf("<"));
    },
    text:function(){
        return this.xmlElem.text == undefined ? this.xmlElem.textContent : this.xmlElem.text;
    },
    attr:function(att){
        return this.xmlElem.getAttribute(att);
    },
    ownerDocument:function(){
        return new XMLDoc(this.ownerDocument);
    },
    documentElement:function(){
        return new XMLElem(this.xmlDoc.documentElement);
    },
    childNodes:function(){
        var ns = [];
        var cs = this.xmlDoc.childNodes;
        for (var i = 0, l = cs.length; i < l; i++){
            ns.push(new XMLElem(cs[i]));
        }
        return ns;
    }
}
//$(document).ready(function(){
//扩展普通属性
"nodeType|nodeValue|nodeName|tagName".split('|').each(function(n){
    XMLElem.prototype[n] = function(v){
        return this.xmlElem[n];
    }
});
//扩展节点值属性
"parentNode|firstChild|lastChild|nextSibling|previousSibling".split('|').each(function(n){
    XMLDoc.prototype[n] = function(){
        return this.documentElement()[n]();
    }
    XMLElem.prototype[n] = function(){
        return new XMLElem(this.xmlElem[n]);
    }
});
//扩展方法
"load".split('|').each(function(n){
    XMLDoc.prototype[n] = function(){
        var a = [];
        for (var i = 0, l = arguments.length; i < l; i++){
            a.push("arguments[" + i + "]");
        }
        return eval("this.xmlDoc[n](" + a.join(",") + ")");
    }
});
//});

//var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();


//window.onload = function(){
//var a=2;
////var doc = new XMLDoc();
////alert(doc.createElement("UU"));
////doc.loadXML("<xml><xml a='d'>jkl</xml><xml>jkl</xml></xml>");
//doc.load("XMLFile.xml");
//var node = doc.selectSingleNode("/xml");
//alert(node.nodeName());
//var ns = node.selectNodes("xml");
//alert(ns[0].xml());
//}
////var HttpRequest = Class.create();
//HttpRequest.prototype = {
//    httpRequest:null,
//    _construct : function (){
//        
//    }
//}
//var Ajax = Class.create();
//Ajax.prototype = {
//    httpRequest: null,
//    options:{
//        method:       'post',
//        async: true,
//        contentType:  'application/x-www-form-urlencoded',
//        encoding:     'UTF-8',
//        params:   '',
//        text: '正在加载中...',
//        thisObj:window,
//        args: null,
//        loading: true,
//        type: 'xml',
//        timeout:-1,
//        onError: Ctn.fn.emptyFn,
//        onSuccess: Ctn.fn.emptyFn,
//        onComplete: Ctn.fn.emptyFn
//    },
//    _construct : function (option){
//        this.httpRequest = Ctn.fn.tryeach(
//        function() {return new XMLHttpRequest()},
//        function() {return new ActiveXObject('Msxml2.XMLHTTP')},
//        function() {return new ActiveXObject('Microsoft.XMLHTTP')}
//        );
//    },
//    request: function(){
//    },
//    onSuccess: function(){
//    },
//    onError: function(){
//    }
//}
